<template>
  <GdHackDynmicComp
    :component-file-path="componentFilePath"
    :root-path-type="rootPathType"
    :component-props="componentProps"
    :remoteCompLoad="remoteCompLoad"
    :isAllowRemoteLoad="isAllowRemoteLoad"
    v-bind="$attrs"
  ></GdHackDynmicComp>
</template>
<script setup>
  import loadRemoteComponent from "./loadRemoteComponent.js";
  import { globalConfig } from "@gd-accbuild-core/config";
  import { getQueryString } from "@gd-accbuild-core/config/utils";
  import { computed } from "vue";
  import { useStore } from "@gd-accbuild-core/store/hackVueStore";
  const _store = useStore();

  const props = defineProps({
    componentFilePath: {
      type: String,
    },
    rootPathType: {
      type: String,
      default: "customize",
      validator(prop) {
        return ["customize", "pages"].includes(prop);
      },
    },
    componentProps: {
      type: Object,
      default: () => ({}),
    },
  });
  const hasCompile = computed(() => {
    if (
      _store &&
      _store.state.hasOwnProperty("settings") &&
      Array.isArray(_store.state.settings.allowRemoteLoadComps)
    ) {
      return _store.state.settings.allowRemoteLoadComps.includes(props.componentFilePath);
    } else {
      return true;
    }
  });
  // 非平台内置自定义组件 且 没有编译标记 才允许远程加载;
  const isAllowRemoteLoad = computed(() => {
    //console.log(props.componentFilePath,'打印')
    return (
      typeof props.componentFilePath === "string" && !props.componentFilePath.startsWith("gd/") && !hasCompile.value
    );
  });
  const remoteCompLoad = () => {
    let url = "";
    if (process.env.PUBLIC_TARGET === "Designer") {
      const proxyUrl = getQueryString("proxyUrl")
        .replace(/(.*)\/gd-sys/, "$1")
        .replace("fc-", "static-");
      const baseStaticPrefixPath = getQueryString("baseStaticPrefixPath")
        ? getQueryString("baseStaticPrefixPath")
        : globalConfig["BASE_STATIC_PREFIX_PATH"];
      url = `${proxyUrl}${baseStaticPrefixPath}`;
    } else {
      url = `${location.origin}${globalConfig["BASE_STATIC_PREFIX_PATH"]}`;
    }
    return loadRemoteComponent(`${url}/static/components/customize/${props.componentFilePath}`);
  };
</script>
